import java.io.*;
import java.sql.*;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;

public class AddItem extends HttpServlet
{
	private String url, sql;
	private Connection conn;
	private PreparedStatement ps;
	protected DbConnection dbconnect; 
	
    
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
    {	
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();
    	String iCode = req.getParameter("itemCode").trim();
		String iName = req.getParameter("itemName").trim();
		String iDetail = req.getParameter("itemDetail").trim();
		String cCode = req.getParameter("categoryCode").trim();
		String iQuantity = req.getParameter("itemQuantity").trim();
		boolean proceed = false;
		
		if(iCode != null && iName != null && iDetail != null && cCode != null && iQuantity != null)
			if(iCode.length() > 0 && iName.length() > 0 && iDetail != null && cCode != null && iQuantity != null)
				proceed = true;
		
    	try 
    	{
    		dbconnect = new DbConnection();
			Class.forName(dbconnect.getDriver());
			conn = dbconnect.getConnection();
    	
    		sql = "INSERT INTO item(item_code, item_name, item_detail, category_code, quantity) VALUES(?, ?, ?, ?, ?)";
    		ps = conn.prepareStatement(sql);
    		
    		if(proceed)
    		{
    			ps.setString(1, iCode);
    			ps.setString(2, iName);
    			ps.setString(3, iDetail);
    			ps.setString(4, cCode);
    			ps.setString(5, iQuantity);
    			ps.executeUpdate();
    		}
    		

    	
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
    	finally
    	{
    		try
    		{
    			if(ps != null)
    			{
    				ps.close();
    			}
    			if(conn != null)
    			{
    				conn.close();
    			}
    		}
    		catch(SQLException sqle)
    		{
    			out.println("" + sqle);
    		}
    	
    	        
    	        res.sendRedirect("InventoryList");
    	  
    	}
    	
    	
    	
    }
}